package com.TeamJigsaw.DanteTrail.Views;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import com.TeamJigsaw.DanteTrail.Models.Party;
import com.TeamJigsaw.DanteTrail.Views.Interfaces.GameViewInterface;

public class GameView extends JPanel implements GameViewInterface{
	private JLabel lblPlayerStatus, lblNewLabel;
	
	private JComboBox paceBox, rationsBox;
	
	private JButton btnHunt, btnPartyInfo, btnTrade, btnRest;
	
	private JButton btnMap;
	
	private JLabel lblDistanceTravelled;
	
	private JLabel lblDistanceLeft;
	
	private JLabel lblFoodLeft;
	
	private JLabel lblNewLabel_1;
	
	private JLabel lblNewLabel_2;
	
	private JLabel lblNewLabel_3;
	
	private String[] rations, paces;
	
	private JButton btnMove;
	
	private JLabel lblPace;
	
	private JLabel lblRations;
	
	private GameScreenDisplay gameScreenDisplay;
	
	private String htmlStart, htmlEnd;
	private JButton btnWagon;
	private JButton btnSave;
	
	/**
	 * Create the panel.
	 */
	public GameView() {
		setSize(new Dimension(700,700));
		setLayout(new MigLayout("", "[10:10:10][100:100:100][grow][][][10:10:10][][10:10:10][][10:10:10][][10:10:10][][10:10:10][]", "[30:30:30][][20:20:20][310:310:310][][30:30:30][][30:30:30][][30:30:30][][]"));
		htmlStart = "<html><font size=\"4\" color=\"#FFFFFF\"><b>";
		htmlEnd = "</b></font></html>";
		initialize();
		

	}
	
	public void initialize()
	{
		
		rations = new String[]{"None", "Starving", "Meager", "Normal","Well Fed"};
	    paces = new String[]{"Blazing","Fast", "Normal", "Slow"};
		
		lblPlayerStatus = new JLabel(htmlStart+"Player Status:"+htmlEnd);
		add(lblPlayerStatus, "cell 1 1");
		
		lblNewLabel = new JLabel(htmlStart+"New label"+htmlEnd);
		add(lblNewLabel, "cell 2 1");
		
		btnSave = new JButton("Save");
		add(btnSave, "cell 6 1");
		
		btnMap = new JButton("Map");
		add(btnMap, "cell 8 1");
		
		gameScreenDisplay = new GameScreenDisplay();
		add(gameScreenDisplay, "cell 2 3 9 1,grow");
		
		btnPartyInfo = new JButton("Party Info");
		add(btnPartyInfo, "cell 10 1");
		
		btnWagon = new JButton("Wagon");
		add(btnWagon, "cell 12 1");
		
		
		lblDistanceTravelled = new JLabel(htmlStart+"Distance Travelled"+htmlEnd);
		add(lblDistanceTravelled, "cell 2 4");
		
		lblNewLabel_1 = new JLabel("New label");
		add(lblNewLabel_1, "cell 8 4");
		
		lblDistanceLeft = new JLabel(htmlStart+"Distance Left"+htmlEnd);
		add(lblDistanceLeft, "cell 2 6");
		
		lblNewLabel_2 = new JLabel(htmlStart+"N/A"+htmlEnd);
		add(lblNewLabel_2, "cell 8 6");
		
		lblFoodLeft = new JLabel(htmlStart+"Food Left"+htmlEnd);
		add(lblFoodLeft, "cell 2 8");
		
		lblNewLabel_3 = new JLabel("New label");
		add(lblNewLabel_3, "cell 8 8");
		
		lblPace = new JLabel(htmlStart+"Pace"+htmlEnd);
		add(lblPace, "cell 2 10");
		
		lblRations = new JLabel(htmlStart+"Rations"+htmlEnd);
		add(lblRations, "cell 4 10");
		
		paceBox = new JComboBox(paces);
		add(paceBox, "cell 2 11,growx");
		
		rationsBox = new JComboBox(rations);
		add(rationsBox, "cell 4 11,alignx right");
		
		btnHunt = new JButton("Hunt");
		add(btnHunt, "cell 6 11");
		
		btnTrade = new JButton("Trade");
		add(btnTrade, "cell 8 11");
		
		btnRest = new JButton("Rest");
		add(btnRest, "cell 10 11");
		
		btnMove = new JButton("Move");
		add(btnMove, "cell 12 11");
	}
	
	public void setDisplay(Party party)
	{
		//display = new GameScreenDisplay(party.getParty());
		//add(display, "cell 2 3,grow");
	}

	@Override
	public void setMapButtonListener(ActionListener listen) {
		btnMap.addActionListener(listen);
		
	}

	@Override
	public void setRestButtonListener(ActionListener listen) {
		btnRest.addActionListener(listen);
		
	}

	@Override
	public void setTradeButtonListener(ActionListener listen) {
		btnTrade.addActionListener(listen);
		
	}

	@Override
	public void setPartyInfoButtonListener(ActionListener listen) {
		btnPartyInfo.addActionListener(listen);
		
	}

	@Override
	public void setMoveButtonListener(ActionListener listen) {
		btnMove.addActionListener(listen);
		
	}

	@Override
	public void setHuntButtonListener(ActionListener listen) {
		btnHunt.addActionListener(listen);
		
	}

	@Override
	public int getPace() {
		if(paceBox.getSelectedItem() == null) {
			return -1;
		}
		else if(paceBox.getSelectedItem().equals("Slow")) {
			return 5;
		}
		else if(paceBox.getSelectedItem().equals("Normal")) {
			return 10;
		}
		else if(paceBox.getSelectedItem().equals("Fast")) {
			return 15;
		}
		else if(paceBox.getSelectedItem().equals("Blazing")) {
			return 20;
		}
		return 0;
	}

	@Override
	public int getRation() {
		if(rationsBox.getSelectedItem() == null) {
			return -1;
		}
		else if(rationsBox.getSelectedItem().equals("None")) {
			return 0;
		}
		else if(rationsBox.getSelectedItem().equals("Starving")) {
			return 1;
		}
		else if(rationsBox.getSelectedItem().equals("Normal")) {
			return 3;
		}
		else if(rationsBox.getSelectedItem().equals("Well Fed")) {
			return 4;
		}
		else if(rationsBox.getSelectedItem().equals("Meager")) {
			return 2;
		}
		return 0;
	}

	@Override
	public void setStatus(String status) {
		lblNewLabel.setText(htmlStart+status+htmlEnd);
		
	}

	@Override
	public void setDistanceTravelled(int distance) {
		lblNewLabel_1.setText(htmlStart+distance+htmlEnd);
		
	}

	@Override
	public void setDistanceToCheckPoint(int distance) {
		lblNewLabel_2.setText(htmlStart+distance+htmlEnd);
		
	}

	@Override
	public void setRationsLeft(int rations) {
		lblNewLabel_3.setText(htmlStart+rations+htmlEnd);
		
	}
	
	@Override
	public boolean listenersExist() {
		if(btnRest.getActionListeners().length > 0)
		{
			return true;
		}
		return false;
	}

	@Override
	public void disableButtons() {
		btnHunt.setEnabled(false);
		btnPartyInfo.setEnabled(false);
		btnTrade.setEnabled(false);
		btnRest.setEnabled(false);
		btnMap.setEnabled(false);
		rationsBox.setEnabled(false);
		paceBox.setEnabled(false);
		btnWagon.setEnabled(false);
		btnSave.setEnabled(false);
		
		
	}

	@Override
	public void enableButtons() {
		btnHunt.setEnabled(true);
		btnPartyInfo.setEnabled(true);
		btnTrade.setEnabled(true);
		btnRest.setEnabled(true);
		btnMap.setEnabled(true);
		rationsBox.setEnabled(true);
		paceBox.setEnabled(true);
		btnWagon.setEnabled(true);
		btnSave.setEnabled(true);
		
	}

	@Override
	public void setMoveText(String text) {
		btnMove.setText(text);
		
	}
	
	@Override
	public String getMoveText(){
		return btnMove.getText();
	}

	@Override
	public void disableRations() {
		rationsBox.setSelectedIndex(5);
		rationsBox.setEnabled(false);
		
	}

	@Override
	public void enableRations() {
		rationsBox.setEnabled(true);
		
	}

	@Override
	public void disableRest() {
		btnRest.setEnabled(false);
		
	}

	@Override
	public void enableRest() {
		btnRest.setEnabled(true);
		
	}
	
	@Override
	public GameScreenDisplay getDisplay()
	{
		return gameScreenDisplay;
	}

	@Override
	public void setRation(int r) {
		rationsBox.setSelectedIndex(r);
		
	}

	@Override
	public void setPace(int p) {
		if(p == 5)
			paceBox.setSelectedIndex(3);
		else if(p == 10)
			paceBox.setSelectedIndex(2);
		else if(p == 15)
			paceBox.setSelectedIndex(1);
		else if(p == 20)
			paceBox.setSelectedIndex(0);
		
	}

	@Override
	public void setWagonListener(ActionListener listen) {
		btnWagon.addActionListener(listen);
		
	}

	@Override
	public void removeListeners() {
		btnHunt.removeActionListener(btnHunt.getActionListeners()[0]);
		btnPartyInfo.removeActionListener(btnPartyInfo.getActionListeners()[0]);
		btnTrade.removeActionListener(btnTrade.getActionListeners()[0]);
		btnRest.removeActionListener(btnRest.getActionListeners()[0]);
		btnMap.removeActionListener(btnMap.getActionListeners()[0]);
		btnWagon.removeActionListener(btnWagon.getActionListeners()[0]);
		btnSave.removeActionListener(btnSave.getActionListeners()[0]);
	}
	
	@Override
	public void paintComponent(Graphics g) {
		ImageIcon img = new ImageIcon("resources\\stone_texture.png");
	    g.drawImage(img.getImage(), 0, 0, null);
	  }

	@Override
	public void saveListener(ActionListener listen) {
		btnSave.addActionListener(listen);
		
	}
	
	public void paintAgain()
	{
		updateUI();
		repaint();
		requestFocus();
	}

}
